﻿@using SmartAnalytics.Report.Extendsions
@model List<SmartAnalytics.Services.Models.OriginPagePageItem>
@{
    ViewBag.Title = "来源页面";
}

<input type="hidden" id="date" name="date" value="@ViewBag.Date" />
<input type="hidden" id="domain" value="@ViewBag.SiteDomain" />

<div class="row">

    @Html.Partial("_PartialMenu")

    <div class="col-md-9 ">

        <ol class="breadcrumb">
            <li><a href="/">SmartAnalytics</a></li>
            <li><a href="/Origin">来源分析</a></li>
            <li class="active">来源页面</li>
        </ol>

        <!--网站域名-->
        @Html.Partial("_PartialSiteDomain", ViewBag.AllDomain as List<SmartAnalytics.Entities.Domain>)

        <div class="callout callout-primary">
            <div class="btn-group" id="tool-list">
                <a class="btn btn-default btn-group-lg" data-date="@DateTime.Now.ToString("yyyy-MM-dd")"
                   href="/Origin/OriginPage?date=@DateTime.Now.ToString("yyyy-MM-dd")&domain=@ViewBag.SiteDomain">今日</a>

                <a class="btn btn-default btn-group-lg" data-date="@DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")"
                   href="/Origin/OriginPage?date=@DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")&domain=@ViewBag.SiteDomain">昨日</a>

                <a class="btn btn-default btn-group-lg" data-date="@DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd")"
                   href="/Origin/OriginPage?date=@DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd")&domain=@ViewBag.SiteDomain">前天</a>
            </div>
        </div>

        <!--图表栏-->
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">统计图表(@ViewBag.Date)</h3>
            </div>
            <div class="panel-body">
                <div id="map-OriginPage-Left" style="height: 300px; border-bottom: 1px solid #ddd;"></div>
                <div id="map-OriginPage-Right" style="height: 300px;"></div>
            </div>
        </div>

        <!--表格栏-->
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">统计报表(@ViewBag.Date)</h3>
            </div>

            <table class="table table-hover table-striped table-condensed table-bordered center">
                <thead>
                    <tr>
                        <th class="center">序号</th>
                        <th class="center">来源页面</th>
                        <th class="center">来访次数</th>
                    </tr>
                </thead>
                <tbody>
                    @for (var i = 0; i < Model.Count; i++)
                    {
                        <tr>
                            <td>
                                @(i + 1)
                            </td>
                            <td style="text-align: left;">
                                <a href="@Model[i].OriginUrl" target="_blank">@Model[i].OriginUrl.MaxSubstring(50)</a>
                            </td>
                            <td>
                                @Model[i].TotalCount
                            </td>
                        </tr>
                    }
                </tbody>
            </table>

        </div>

        <div class="alert alert-info" role="alert">
            说明：功能说明性内容。
        </div>

    </div>
</div>

<script type="text/javascript">

    require.config({
        paths: {
            echarts: '/Scripts/ECharts'
        }
    });
    //渲染ECharts
    function RenderECharts(chartsArea, chartOption) {
        require([
        'echarts',
        'echarts/chart/line',
        'echarts/chart/pie'
        ], function (ec) {
            var myChart = ec.init(document.getElementById(chartsArea));
            myChart.setOption(chartOption);
        });
    }

    $(function () {

        $('[data-toggle="popover"]').popover({ animation: true, trigger: 'hover' });

        //获取系统常量
        var domain = $('#domain').attr('value');
        var date = $('#date').attr('value');


        //来源分析-来源页面-按小时统计
        TongJiApi.OriginPageHour(date, domain, function (data) {

            //折线图
            var option = getOptionLeft(date, domain, data);

            RenderECharts('map-OriginPage-Left', option);

        });

        //来源分析-来源页面-按日域名统计
        TongJiApi.OriginPageDay(date, domain, function (data) {

            //饼图
            var option = getOptionRight(date, domain, data);

            RenderECharts('map-OriginPage-Right', option);

        });

    });

    function getOptionLeft(day, domain, data) {
        //折线图
        var option = {
            tooltip: {
                trigger: 'axis',
                formatter: function (params) {
                    var tip = '';
                    if (params.length > 0) {
                        var hour = parseInt(params[0].name);
                        var prefix = '';
                        if (hour > 0 && hour < 05) {
                            prefix = '凌晨';
                        } else if (hour >= 05 && hour < 08) {
                            prefix = '早上';
                        } else if (hour >= 08 && hour < 12) {
                            prefix = '上午';
                        } else if (hour >= 12 && hour < 13) {
                            prefix = '中午';
                        } else if (hour >= 13 && hour < 18) {
                            prefix = '下午';
                        } else if (hour >= 18 && hour < 20) {
                            prefix = '傍晚';
                        } else if (hour >= 20) {
                            prefix = '夜晚';
                        }
                        tip += prefix + params[0].name + '时<br />';
                    }
                    for (var i = 0; i < params.length; i++) {
                        tip += params[i].seriesName + '：' + params[i].value + '<br />';
                    }
                    return tip;
                }
            },
            legend: {
                data: []
            },
            calculable: true,
            xAxis: [
                {
                    type: 'category',
                    name: '时段',
                    boundaryGap: false,
                    data: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
                }
            ],
            yAxis: [
                {
                    name: '数量',
                    type: 'value'
                }
            ],
            series: []
        };

        $.each(data, function (k, v) {

            option.legend.data.push(v.OriginDomain);

            option.series.push({
                name: v.OriginDomain,
                type: 'line',
                data: v.Data
            });

        });

        return option;
    }

    function getOptionRight(day, domain, data) {

        //饼图
        var option = {
            tooltip: {
                trigger: 'item',
                formatter: "{b} : {c}<br/>总量占比：{d}%"
            },
            legend: {
                orient: 'vertical',
                x: 'left',
                data: []
            },
            calculable: true,
            series: [
                {
                    name: '来源域名',
                    type: 'pie',
                    center: ['60%', '55%'],
                    data: []
                }
            ]
        };

        $.each(data, function (k, v) {

            option.legend.data.push(v.OriginDomain);

            option.series[0].data.push({ value: v.TotalCount, name: v.OriginDomain });
        });

        return option;
    }

</script>


